package com.zhongan.user.controller;

import com.zhongan.common.dto.ResultBase;
import com.zhongan.user.api.UserApi;
import com.zhongan.user.api.dto.UserDto;
import com.zhongan.user.api.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户体系服务
 * @author za-shaojian
 */
@RestController("userController")
@RequestMapping("/user")
public class UserController implements UserService {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private UserApi userApi;

    @Autowired
    private DiscoveryClient client;

    /**
     * @author za-shaojian
     * @create time 2017-09-16
     * @return
     */
    public ResultBase<UserDto> findByUsername(String userName) {
        UserDto userDto = userApi.findByUsername(userName);
        ResultBase<UserDto> resultBase = new ResultBase<UserDto>();
        resultBase.setValue(userDto);
        return resultBase;
    }

    public ResultBase<Integer> add(@RequestParam Integer a, @RequestParam Integer b) {
        ServiceInstance instance = client.getLocalServiceInstance();
        ResultBase<Integer> resultBase = new ResultBase<Integer>();
        Integer r = a + b;
        resultBase.setValue(new Integer(r));
        logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
        return resultBase;
    }
}